<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.username" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户名" />
      <el-input v-model="listQuery.userId" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户Id" />
      <el-input v-model="listQuery.mobile" clearable class="filter-item" style="width: 200px;" placeholder="请输入手机号" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" width="100px" label="用户ID" prop="id" sortable />
      <el-table-column align="center" label="用户名" prop="userName" />
      <el-table-column align="center" label="性别" prop="sex" />
      <el-table-column align="center" label="手机号码" prop="phone" />

      <el-table-column align="center" label="拉黑状态" prop="blackList">
        <template slot-scope="scope">
          <el-tag :type="scope.row.blackList ? 'success' : 'error' ">{{ scope.row.blackList==0 ? '在线' : '拉黑状态' }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUserUpdate(scope.row)">拉黑</el-button>
          <el-button type="primary" size="mini" @click="handleUserCancel(scope.row)">取消拉黑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="pageList.count" :page.sync="pageList.pageIndex" :limit.sync="pageList.size" @pagination="getList" />
    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>
  </div>
</template>

<script>
// fetchList, userDetail,
import { UpdateUser, userAccount, updateCancel } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'User',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      page: 0,
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        mobile: undefined,
        userId: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      pageList: {
        count: 0,
        pageIndex: 1,
        size: 10
      }
    //     downloadLoading: false,
    //     userDialogVisible: false,
    //     userDetail:{
    //     }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 查询数据，显示到页面
    getList() {
      this.listLoading = true
      userAccount(this.pageList).then(response => {
        this.list = response.data.data.data
        this.pageList.count = response.data.data.count
        this.code = response.data.code
        if (this.code === 200) {
          // code状态码等于200时，停止加载
          this.listLoading = false
        }
      })
      /* if(this.listQuery.userId){
          userDetail(this.listQuery.userId).then(response => {
            this.list = [];
            if(response.data.data){
              this.list.push(response.data.data)
              this.total = 1
              this.listLoading = false
            }else{
              this.list = []
              this.total = 0
              this.listLoading = false
            }c
          }).catch(() => {
            this.list = []
            this.total = 0
            this.listLoading = false
          })
        }else{
          fetchList(this.listQuery).then(response => {
            this.list = response.data.data.list
            this.total = response.data.data.total
            this.listLoading = false
          }).catch(() => {
            this.list = []
            this.total = 0
            this.listLoading = false
          })
        } */
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['用户名', '手机号码', '性别', '拉黑状态']
          const filterVal = ['userName', 'phone', 'sex', 'blackList']
          excel.export_json_to_excel2(tHeader, this.list, filterVal, '用户信息')
          this.downloadLoading = false
        })
    },
    // handleDetail(row) {
    //   this.userDetail = row
    //   this.userDialogVisible = true
    // },

    // 拉黑用户
    handleUserUpdate(row) {
      UpdateUser(row)
        .then((response) => {
          this.$notify.success({
            title: '成功',
            message: '拉黑用户成功'
          })
          userAccount().then(response => {
            this.list = response.data.data
            this.code = response.data.code
            if (this.code === 200) {
              // code状态码等于200时，停止加载
              this.listLoading = false
            }
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    // 取消拉黑
    handleUserCancel(row) {
      updateCancel(row.id)
        .then((response) => {
          if (response.data.code === 200) {
            this.$notify.success({
              title: '成功',
              message: '取消拉黑用户成功'
            })
            userAccount().then(response => {
              this.list = response.data.data
              this.code = response.data.code
              if (this.code === 200) {
                // code状态码等于200时，停止加载
                this.listLoading = false
              }
            })
          }
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    }

  }
}
</script>
